﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Utility;
using Model;
namespace Service
{
    public class ServerUploadLogService
    {
        public int Add(ServerUploadLog log)
        {
            string sql = "insert into ServerUploadLog values(newid(),@LogSchool,@LogSchoolName,@LogDate,@State,@ActionDate,@FilePath,@FileType,@UploadUser,@UploadUserName)";
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@LogSchool",log.LogSchool),
                new SqlParameter("@LogSchoolName",log.LogSchoolName),
                new SqlParameter("@LogDate",log.LogDate),
                new SqlParameter("@State",log.State),
                new SqlParameter("@ActionDate",log.ActionDate),
                new SqlParameter("@FilePath",log.FilePath),
                new SqlParameter("@FileType",log.FileType),
                new SqlParameter("@UploadUser",log.UploadUser),
                new SqlParameter("@UploadUserName",log.UploadUserName),
            };
            int n = SqlHelper.ExecuteNonQuery(Utility.WebConn.connString, CommandType.Text, sql, sp);
            return n;
        }
        public DataTable GetLog(int pageIndex, int pageSize, out int count)
        {
            string sql=@"select top(@pageSize) * from ServerUploadLog where logid not in(select top(@pageSize*@pageIndex) logid from ServerUploadLog order by logid) order by logid";
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@pageSize",pageSize),
                new SqlParameter("@pageIndex",pageIndex),
            };
            string sql2 = "select count(*) from ServerUploadLog";
            count = (int)SqlHelper.ExecuteScalar(Utility.WebConn.connString, CommandType.Text, sql2);
            DataTable dt= SqlHelper.ExecuteDataset(Utility.WebConn.connString,CommandType.Text,sql,sp).Tables[0];
            return dt;
        }
        public DataTable GetLog(int pageIndex, int pageSize, out int count, string filedname, string filedvalue)
        {
            string sql = "select top(@pageSize) * from ServerUploadLog where " + filedname + "=@filedvalue and logid not in(select top(@pageSize*@pageIndex) logid from ServerUploadLog where " + filedname + "=@filedvalue order by logid) order by logid";
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@pageSize",pageSize),
                new SqlParameter("@filedvalue",filedvalue),
                new SqlParameter("@pageIndex",pageIndex),
                
            };

            DataTable dt= SqlHelper.ExecuteDataset(Utility.WebConn.connString,CommandType.Text,sql,sp).Tables[0];
            string sql2 = "select count(*) from ServerUploadLog where "+ filedname + "=@filedvalue" ;
            SqlParameter[] sp2 = new SqlParameter[]{
                new SqlParameter("@filedvalue",filedvalue),
            };
            count = (int)SqlHelper.ExecuteScalar(Utility.WebConn.connString, CommandType.Text, sql2,sp2);
            return dt;
        }
        public DataTable GetLogBy(string filedname,string filedvalue)
        {
            string sql = "select * from ServerUploadLog where " + filedname + "=@filedvalue order by logid desc";
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@filedvalue",filedvalue),
                
            };

            DataTable dt= SqlHelper.ExecuteDataset(Utility.WebConn.connString,CommandType.Text,sql,sp).Tables[0];
            return dt;
        }
    }
}
